<?php
/*  
  Copyright (c) 2010-02 SongCMS 
  SongCMS All Rights Reserved. 
  Support:www.SongCMS.com
  QQ:544255858   19951025
  Author:Song  Version:3.17
  Date:2010-08-12 09:28:32
*/

include_once('Admin.php');
$ColumnType = 2; //栏目类型:0栏目内容分类,1新闻分类,2产品分类

if ($_POST['Action'] == 'Add'){
	//添加
	$ColumnSort = is_numeric($_POST['ColumnSort']) ? $_POST['ColumnSort'] : 0;
	$ColumnLevel = count(explode('|',$_POST['ColumnRela'])); //获取当前栏目级数
	if($_POST['ColumnRela'] != '') { $ColumnLevel++; }
	$SQL = "INSERT INTO `" . $dbprefix . "column` (`ColumnType`,`ColumnLang`,`ColumnRela`,`ColumnName`,`ColumnLevel`,`ColumnSort`,`ColumnTime`)";
	$SQL .= "VALUES ('" . $ColumnType . "','" . $Lang . "','" .$_POST['ColumnRela'] . "','" . $_POST['ColumnName'] . "','" . $ColumnLevel . "','" . $ColumnSort . "',NOW())";
	if ($db->ExecuteSQL($SQL)){
		//获取当前添加的ID
		if($Rs = $db->SelectSQL("SELECT `ID`,`ColumnRela` FROM `" . $dbprefix . "column` ORDER BY `ID` DESC LIMIT 0,1;")){
			$ColumnRela = $Rs[0][1] == '' ? $Rs[0][0] : $_POST['ColumnRela'] . '|' . $Rs[0][0]; //当前插入的关系路径
			if($db->ExecuteSQL("UPDATE `" . $dbprefix . "column` SET `ColumnRela` = '" . $ColumnRela . "' WHERE `ID` = " . $Rs[0][0] . " LIMIT 1 ")){
				unset($Rs,$SQL,$ColumnLevel,$ColumnRela);
				AdminLog(2,$Lang,'添加产品类别，结果：操作成功！'); 
				MsgBox (1,'产品类别添加成功！','1');
			} else {
				unset($Rs,$SQL,$ColumnLevel,$ColumnRela);
				AdminLog(2,$Lang,'添加产品类别，结果：操作失败！'); 
				MsgBox (1,'产品类别添加失败！请检查您提交的数据中是否含有非法字符！','1');
			}
		}
	}
}
if ($_GET['Action'] == 'Del'){
	if(!is_numeric(($_GET['ID']))) MsgBox (1,'您提交的参数有误！','-1');
	if(in_array($_GET['ID'],$NotDelProductColumn)){AdminLog(3,$Lang,'尝试删除受保护的产品类别(ID:' . $_GET['ID'] . ')，结果：操作被拒绝！'); MsgBox (1,'非法操作，该类别为系统默认栏目，必须保留，您可以修改该类别！','-1');}
	$Count = $db->SelectSQL("SELECT count(ID) FROM `" . $dbprefix . "column` WHERE `ColumnRela` LIKE '%" . $_GET['ID'] . "%' && ID != '" . $_GET['ID'] . "'");
	if($Count[0][0]>0) {AdminLog(3,$Lang,'删除产品类别(ID:' . $_GET['ID'] . ')失败，原因：要删除的类别下有子类存在！'); MsgBox (1,'操作失败，该类别下有子类存在，请先删除下属子类别！','-1');}
	$Count = $db->SelectSQL("SELECT count(ID) FROM `" . $dbprefix . "product` WHERE `ProductRela` LIKE '%" . $_GET['ID'] . "%'");
	if($Count[0][0]>0) {AdminLog(3,$Lang,'删除产品类别(ID:' . $_GET['ID'] . ')失败，原因：要删除的类别下有产品内容存在！'); MsgBox (1,'操作失败，该类别下有产品存在，请先删除该类别下的产品内容！','-1');}
	if($db->ExecuteSQL("DELETE FROM `" . $dbprefix . "column` WHERE `ID` = '" . $_GET['ID'] . "' LIMIT 1")){
		unset($Count);
		AdminLog(3,$Lang,'删除产品类别(ID:' . $_GET['ID'] . ')，结果：操作成功！'); 
		MsgBox (1,'操作成功！','ProductColumnManage.php?Lang=' . $Lang);
	} else {
		unset($Count);
		AdminLog(3,$Lang,'删除产品类别(ID:' . $_GET['ID'] . ')，结果：操作失败！'); 
		MsgBox (1,'操作失败！','-1');
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="./images/adminstyle.css" rel="stylesheet" type="text/css" />
<link rel="Shortcut Icon" href="../favicon.ico">
<link rel="Bookmark" href="../favicon.ico">
<script language="javascript">
function trim(str) {
	return str.replace(/(^[\s\u3000]*)|([\s\u3000]*$)/g,"");
}
function FormCheck(){
	if(trim(document.form1.ColumnName.value)==""){
		alert('类别名称必须填写！');
		document.form1.ColumnName.focus();
		return false;
	}
}
</script>
<script src="../js/csshovernotie6.js" type="text/javascript"></script>
</head>
<body>
<br />
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr class="Notodd"><td><?php TopLang();?></td>
	<td align="right">
		<input type="button" value="添加产品" onClick="javascript:location.href='ProductAdd.php?Lang=<?php echo $Lang?>'">
		<input type="button" value="产品管理" onClick="javascript:location.href='ProductManage.php?Lang=<?php echo $Lang?>'">		
	</td>	
	</tr>
</table>
<table width="98%" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
  <form id="form1" name="form1" method="post" action="" onsubmit="return FormCheck()">
  <tr class="odd">
    <td colspan="3" align="center" class="Main_Title">添加产品类别<?php echo $OnWebLangName?></td>
  </tr>
  <tr class="odd">
  	<td width="16%" align="right">所属类别：</td>
  	<td width="38%">
<select name="ColumnRela" id="ColumnRela">
<option selected="selected" value="">==作为一级类别==</option>
<?php
$Rs = $db->SelectSQL("SELECT * FROM `" . $dbprefix . "column` WHERE ColumnType = $ColumnType && ColumnLang = $Lang;",1);
@$Rs = sdb::column_sort($Rs);
foreach($Rs as $i => $n) {
	Html($n);
	for ($x=1;$x<$n['ColumnLevel'];$x++){$Spaces .= '├┈┈';} //加与栏目级数相同的空格
	$n['ColumnLevel'] > 1 ? $Spaces = $Spaces . $n['ColumnName'] : $Spaces = $n['ColumnName']; //一级栏目前不加空格符
	echo '<option value="' . $n['ColumnRela'] . '">' . $Spaces . '</option>' . "\n";
	unset($Spaces);
}
?>  		
</select>
</td>
  	<td class="ippv">支持无限级分类</td>
  	</tr>
  <tr class="odd">
  	<td align="right">类别名称：</td>
  	<td><input name="ColumnName" type="text" class="input2" id="ColumnName" maxlength="100" />
  		<span class="red">*</span></td>
  	<td>&nbsp;</td>
  	</tr>
  <tr class="odd">
  	<td height="30" align="right">自定义排序：</td>
  	<td><input name="ColumnSort" type="text" id="ColumnSort" value="0" size="5" maxlength="3" /></td>
  	<td class="ippv">升序(值越小越靠前)</td>
  	</tr>		
  <tr class="odd">
    <td height="30" align="right">&nbsp;</td>
    <td><input type="submit" name="button" id="button" value="提交" />
    	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    	<input type="reset" name="button2" id="button2" value="重置" />
    	<input name="Action" type="hidden" id="Action" value="Add" /></td>
    <td>&nbsp;</td>
  </tr>
    </form>
  </table>
<br />
<table width="98%" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
<tr class="odd">
<td colspan="5" align="center" class="Main_Title">产品类别管理<?php echo $OnWebLangName?></td>
</tr>
<tr class="odd">
<td align="center" height="22"><strong>类别ID号</strong></td>
<td align="center"><strong>类别名称</strong></td>
<td align="center"><strong>添加时间</strong></td>
<td align="center"><strong>排序</strong></td>
<td align="center"><strong>管理选项</strong></td>
</tr>
<?php
if(!is_array($Rs)) $Rs = array();
foreach($Rs as $i => $n) {
	echo !in_array($n['ID'],$NotDelPageColumn) ? '<tr class="odd">' : '<tr class="odd" id="ippv">'; //检测是否为禁止删除的栏目
	for ($x=1;$x<$n['ColumnLevel'];$x++){$Spaces .= '├┈┈┈';} //加与栏目级数相同的空格
	$n['ColumnLevel'] > 1 ? $Spaces = $Spaces . $n['ColumnName'] : $Spaces = $n['ColumnName'];	//一级栏目前不加空格符
	echo '<td align="center">' . $n['ID'] . '</td>';
	echo '<td align="left">' . $Spaces . '</td>' . "\n";
	echo '<td align="center">' . $n['ColumnTime'] . '</td>' . "\n";
	echo '<td align="center">' . $n['ColumnSort'] . '</td>' . "\n";
	echo '<td align="center"><a href="ProductColumuUpDate.php?Lang=' . $Lang . '&ID=' . $n['ID'] . '">修改</a> | <a onClick="return confirm(\'您确定要删除该栏目吗？\');" href="?Lang=' . $Lang . '&Action=Del&ID=' . $n['ID'] . '">删除</a></td>' . "\n";
	echo '</tr>' . "\n";
	unset($Spaces,$ColumnTemPage,$ColumnTemDetail);
}
if (count($Rs)==0){
	echo '<tr class="odd"><td align="center" height="35" colspan="6">
	没有相关栏目，请添加！
	</td></tr>';
}
unset($SQL,$Rs,$i,$x);
?>
</table>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="6">
	<tr class="Notodd">
		<td style="color:#333333; line-height:20px;">
注：1、含有子类及产品内容的类别无法删除，必须从最低层开始删起，请先删除相关类别下所有的子类及产品内容；<br />
　　2、灰色(类别名称颜色)类别为系统受保护的类别，防止前台默认数据被删除，可能有部分类别做了防删处理，您将无法删除系统默认的类别；<br />
　　3、系统类别必须保留，您可以对其进行修改、更新、改名、移动等操作；您还可以在系统高级参数设定中取消/添加受保护的类别；		
		</td>
	</tr>
</table>
</body>
</html>